using System;
using System.IO;
using LitJson;
using dotnet;

namespace wspace
{
    public class ServerInfo
    {
        public string user;
        public string host;
        public string pwd;
        public int port;
        public string dir;

        public static ServerInfo GetByConfig(string cfgpath)
        {
            ServerInfo info = null;
            try
            {
                byte[] buf = File.ReadAllBytes(cfgpath);
                info = JsonMapper.ToObject<ServerInfo>(System.Text.Encoding.UTF8.GetString(buf));
            }
            catch (Exception e)
            {
                Utils.ErrorLog(e.Message);
                return null;
            }
            if (info.user == null || info.host == null || info.pwd == null || info.dir == null || info.port == 0)
            {
                Utils.ErrorLog("The configuration file is incorrect.");
                return null;
            }
            return info;
        }
    }
}